package com.aliyun.oss.UserService.impl;

import com.aliyun.oss.UserMapper.AddressBookMapper;
import com.aliyun.oss.UserService.AddressBookService;
import com.aliyun.oss.pojo.Entity.addressBook;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper,addressBook> implements AddressBookService {
    @Override
    public void addAddressBook(Integer id, addressBook addressBook) {
        addressBook.setUserId(Long.valueOf(id));
        save(addressBook);
    }

    @Override
    public void setDefault(Integer id, Integer isDefault) {
//        先用id将所有地址查询出来，然后遍历置0
        List<addressBook> list = list(new QueryWrapper<addressBook>().eq("user_id",id));
        for (addressBook addressBook : list) {
            addressBook.setIsDefault(0);
            updateById(addressBook);
        }
        addressBook addressBook = getOne(new QueryWrapper<addressBook>().eq("id", isDefault));
        addressBook.setIsDefault(1);
        updateById(addressBook);
    }
}
